package com.jfl.utils;

import java.math.BigDecimal;

/**
 * 算术工具类
 * 
 * @author 赵炎
 * @version [V1.00, 2018年12月3日]
 * @since V1.00
 */
public final class MathUtil
{
    /**
     * 默认运算精度
     */
    private static int DEF_SCALE = 10;
    
    private MathUtil()
    {
    }
    
    /**
     * 提供数据类型转换为BigDecimal
     *
     * @param object
     *            原始数据
     * @return BigDecimal
     */
    public static final BigDecimal bigDecimal(Object object)
    {
        if (object == null)
        {
            throw new NullPointerException();
        }
        BigDecimal result;
        try
        {
            result = new BigDecimal(String.valueOf(object).replaceAll(",", ""));
        }
        catch (NumberFormatException e)
        {
            throw new NumberFormatException("Please give me a numeral.Not " + object);
        }
        return result;
    }
    
    public static final Double multiply(Object obj1, Object obj2)
    {
        BigDecimal result = bigDecimal(obj1).add(bigDecimal(obj2));
        return result.setScale(DEF_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    
    /**
     * 获取start到end区间的随机数,不包含start+end
     *
     * @param start
     * @param end
     * @return
     */
    public static final BigDecimal getRandom(double start, double end)
    {
        return new BigDecimal(start + Math.random() * (end - start));
    }
    
}
